<?php 
/**
 * 地址模型
 * @author 杨帆 
 */
class AddressModel extends Model{
    //表明
    public $table = 'address';
    //自动验证
    public $validate = array(
        array('consignee','nonull','收货人不能为空',2,3),
        array('address','nonull','详细收货地址不能为空',2,3),
        array('mobile','nonull','手机号码不能为空',2,3),
    );
    
    /**
     * 异步添加收货地址
     */
    public function _add(){
        if(!$this->create()) return false;
        return true;
    }
    
     /**
     * 添加收货地址
     */
    public function addData(){
        if(!$this->create()) return false;
        //处理城市地址数据
            $province = Q('post.province',0,'intval');
            $province = M('area')->where("area_id={$province}")->getfield('area_name');
            $city = Q('post.city');
            $city = M('area')->where("area_id={$city}")->getfield('area_name');
            $county = Q('post.county');
            $county = M('area')->where("area_id={$county}")->getfield('area_name');
            //如果合法,则执行以下操作;
            $data = array(
               'consignee'=>Q('post.consignee'),
               'address'=>$province.'-'.$city.'-'.$county.'-'.Q('post.address').'-'.Q('post.zipCode'),
               'mobile'=>Q('post.mobile'),
               'user_uid' =>$_SESSION['uid'],
            );
        $this->add($data);
        return true;
    }
    
    /**
     * 编辑地址
     */
    public function _edit($addid){
        if(!$this->create()) return false;
        //处理城市地址数据
            $province = Q('post.province',0,'intval');
            $province = M('area')->where("area_id={$province}")->getfield('area_name');
            $city = Q('post.city');
            $city = M('area')->where("area_id={$city}")->getfield('area_name');
            $county = Q('post.county');
            $county = M('area')->where("area_id={$county}")->getfield('area_name');
            //如果合法,则执行以下操作;
            $data = array(
               'consignee'=>Q('post.consignee'),
               'address'=>$province.'-'.$city.'-'.$county.'-'.Q('post.address').'-'.Q('post.zipCode'),
               'mobile'=>Q('post.mobile'),
               'user_uid' =>$_SESSION['uid'],
            );
        $this->where("addid={$addid}")->update($data);
        return true;
    }
 }
 
 
 
 
 
 
 
 
 
 
 



 ?>